<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Admin\Performer;
use Input;
class PerformerController extends Controller
{
    public function index(){
        $data = Performer::orderBy('id','desc')-> get();
        return view('admin.performer.index',['data' => $data]);
    }

    public function add(){
        if(Input::method() == 'POST'){
            $data = Input::all();
            $result = Performer::insert([
                'avatar_img'		=>	$data['avatar_img'],
                'performer_name'	=>	$data['performer_name'],
                'alias'		        =>	$data['alias'],
                'gender'            =>	$data['gender'],
                'occupation'        =>	$data['occupation'],
                'birthday'	        =>	$data['birthday'],
                'introduction'	    =>	$data['introduction'],
                'area'			    =>	$data['area'],
                'ones_native_heath' =>	$data['ones_native_heath'],
                'blood_type'		=>	$data['blood_type'],
                'constellation'		=>	$data['constellation'],
                'is_director'		=>	$data['is_director'],
            ]);
            session('avatar_img_url',NULL);
            return $result ? '1' : '0';
        }else{
            return view('admin.performer.add');
        }
    }

    public function edit(){
        if(Input::method() == 'POST'){
            $data = Input::all();
            $result = Performer::where('id',$data['id']) -> update([
                'avatar_img'		=>	$data['avatar_img'],
                'performer_name'	=>	$data['performer_name'],
                'alias'		        =>	$data['alias'],
                'gender'            =>	$data['gender'],
                'occupation'        =>	$data['occupation'],
                'birthday'	        =>	$data['birthday'],
                'introduction'	    =>	$data['introduction'],
                'area'			    =>	$data['area'],
                'ones_native_heath' =>	$data['ones_native_heath'],
                'blood_type'		=>	$data['blood_type'],
                'constellation'		=>	$data['constellation'],
                'is_director'		=>	$data['is_director'],
            ]);
            return $result ? '1' : '0';
        }else{
            $id = Input::get('id');
            $data = Performer::where('id',$id) -> first();
            return view('admin.performer.edit',['data' => $data]);
        }
    }

    public function del(){
        $id = Input::get('id');
        $res = Performer::where('id',$id) -> delete();
        $res ? $data['status'] = 200 :  $data['status'] = 0;
        return response() -> json($data);
    }

    public function search(){
        $keyword = Input::get('key');
        $start_time = Input::get('datemin') ? Input::get('datemin') . ' 00:00:00' : '1900-01-01 00:00:00';
        $end_time = Input::get('datemax')? Input::get('datemax') . ' 23:59:59' : date('Y-m-d H:i:s');
        $data = Performer::where('performer_name','LIKE',"%$keyword%") -> whereBetween('birthday',[$start_time,$end_time]) -> get();
        //展示视图，携带数据
        return view('admin.performer.index',['data' => $data, 'key' => $keyword ,'datemin' => Input::get('datemin') , 'datemax' => Input::get('datemax')]);
    }

}